# Copyright 1999-2009 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI="3"

inherit git-2 eutils versionator

DESCRIPTION="D Programming Language 2.0 documentation"

HOMEPAGE="http://d-programming-language.org/"

LICENSE="DMD"
SLOT=${PV/.9999/}
KEYWORDS="~x86 ~amd64"
IUSE=""
EAPI="3"

RESTRICT="mirror"

src_unpack() {
    local DMD_DIR="${WORKDIR}/dmd"
    local DRUNTIME_DIR="${WORKDIR}/druntime"
    local PHOBOS_DIR="${WORKDIR}/phobos"

    EGIT_SOURCEDIR="${WORKDIR}/d-programming-language.org"
    EGIT_REPO_URI="git://github.com/D-Programming-Language/d-programming-language.org.git"
    git-2_src_unpack

    EGIT_COMMIT="v${SLOT}"

    EGIT_SOURCEDIR="${DMD_DIR}.${SLOT}"
    EGIT_REPO_URI="git://github.com/D-Programming-Language/dmd.git"
    git-2_src_unpack

    EGIT_SOURCEDIR="${DRUNTIME_DIR}.${SLOT}"
    EGIT_REPO_URI="git://github.com/D-Programming-Language/druntime.git"
    git-2_src_unpack

    EGIT_SOURCEDIR="${PHOBOS_DIR}.${SLOT}"
    EGIT_REPO_URI="git://github.com/D-Programming-Language/phobos.git"
    git-2_src_unpack

    S="${WORKDIR}"
}

src_prepare() {
    sed -e "s:GIT_HOME=git@github.com\:D-Programming-Language:GIT_HOME=https\://github.com/D-Programming-Language:" \
        -i ${WORKDIR}/d-programming-language.org/posix.mak || die "sed failed"

    sed -e "s:git clone:echo :" \
        -i ${WORKDIR}/d-programming-language.org/posix.mak || die "sed failed"

    sed -e "s:git checkout:echo :" \
        -i ${WORKDIR}/d-programming-language.org/posix.mak || die "sed failed"

    sed -e "s:DMD=dmd:DMD=../dmd.${SLOT}/src/dmd:" \
        -i ${WORKDIR}/d-programming-language.org/posix.mak || die "sed failed"

    sed -e "s:doc -j 4:import doc -j 4:" \
        -i ${WORKDIR}/d-programming-language.org/posix.mak || die "sed failed"

    sed -e "s:\${LATEST}:${SLOT}:" \
        -i ${WORKDIR}/d-programming-language.org/posix.mak || die "sed failed"

    sed -e "s:DRUNTIME_PATH = ../druntime:DRUNTIME_PATH = ../druntime.${SLOT}:" \
        -i ${WORKDIR}/phobos.${SLOT}/posix.mak || die "sed failed"

    sed -e "s:DMD?=dmd:DMD?=../dmd.${SLOT}/src/dmd:" \
        -i ${WORKDIR}/druntime.${SLOT}/posix.mak || die "sed failed"

    sed -e "s:DMD ?= dmd:DMD ?= ../dmd.${SLOT}/src/dmd:" \
        -i ${WORKDIR}/phobos.${SLOT}/posix.mak || die "sed failed"
}

src_compile() {
    local MAKETARGETS
    MAKETARGETS="druntime-release phobos-release html"

    cd "${S}/d-programming-language.org"
    make -f posix.mak ${MAKETARGETS} || die "compile failed!"
}

src_install() {
    dodir /usr/share/doc/${PF}/html
    cp -R ${S}/d-programming-language.org/web/* "${D}"/usr/share/doc/${PF}/html/ || die "install failed!"
}
